﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _08_Lock死锁
{
    class TEST
    {
        object obj1 = new object();
        object obj2 = new object();


        public void Method1()
        {
            lock (obj1)
            {
                Console.WriteLine("Method1锁住obj1");
                lock (obj2)
                {
                    Console.WriteLine("Method1锁住obj2");
                }
            }
        }

        public void Method2()
        {
            lock (obj2)
            {
                Console.WriteLine("Method2锁住obj2");
                lock (obj1)
                {
                    Console.WriteLine("Method2锁住obj1");
                }
            }
        }

    }

    internal class Program
    {
        static void Main(string[] args)
        {
            TEST test = new TEST();

            new Thread(test.Method1) { IsBackground=true }.Start();
            new Thread(test.Method2) { IsBackground = true }.Start();

            Console.Read();
        }

    }
}
